package com.ant.storage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ant.storage.pojo.Department;
import com.ant.storage.mapper.DepartmentMapper;
import com.ant.storage.service.IDepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2022-04-13
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Override
    public String getDepName(Integer id) {
        String str="";
        List<Department> resultList = this.baseMapper.selectList(null);
        Department department = this.baseMapper.selectOne(new QueryWrapper<Department>()
                .eq("department_id", id));
        if(department!=null){
            List<Department> deptUpList = getDeptUpList(resultList, department);
            if(deptUpList==null ||deptUpList.size()==0){
                str = department.getDepartmentName();
            }else{
                Collections.reverse(deptUpList);
                deptUpList.add(department);
                int i = 0;
                for (Department dep : deptUpList) {
                    i++;
                    if (deptUpList.size() == i) {
                        str += dep.getDepartmentName();
                    } else {
                        str += dep.getDepartmentName() + " - ";
                    }
                }
            }
        }else{
            return null;
        }
        return str;
    }


    @Override
    public String getDepNames(Integer id) {
        Department department = this.baseMapper.selectOne(new QueryWrapper<Department>().eq("department_id", id));
        if(department!=null){
            return department.getDepartmentName();
        }
        return null;
    }


    public static List <Department> getDeptUpList(List<Department> deptAll, Department leafNodeDept) {
        if (ObjectUtil.isNotEmpty(leafNodeDept)) {
            List <Department> set =new ArrayList<>();
            Integer parentId = leafNodeDept.getParentId();
            List<Department> parentDepts = deptAll.stream().filter(item -> item.getDepartmentId()
                    .equals(parentId)).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(parentDepts)) {
                Department parentDept = parentDepts.get(0);
                set.add(parentDept);
                List<Department> deptUpTree = getDeptUpList(deptAll, parentDept);
                if (deptUpTree != null) {
                    set.addAll(deptUpTree);
                }
                return set;
            }
        }
        return null;
    }
    public Department getTheTop(List<Department> resultList, Department department) {
        if (department.getParentId() == 1) {
            return department;
        }
        //拿目标bean的 parentid 到结果集中去过滤
        Department subBean = resultList.stream().filter(i -> Objects.equals(i.getDepartmentId(),
                department.getParentId())
        ).findAny().get();
        System.out.println(subBean);
        return subBean;
    }

    @Override
    public  List<Department> getDepartmentListById(Integer did) {
        return searchDepartment(did);
    }
    //定义一个方法查询当前部门及其子部门
    public List<Department> searchDepartment(Integer departmentId){
        //查询当前部门信息
        Department currentDepartment = getCurrentDepartment(departmentId);
        //查询当前部门下的子部门列表
        List<Department> childDepartmentList = getChildDepartmentList(departmentId);
        //如果当前部门不为空
        if(currentDepartment != null){
            //递归查询子部门信息
            for (Department child: childDepartmentList) {
                searchDepartment(child.getDepartmentId());
            }
        }
        return  childDepartmentList;
    }
    //获取当前部门信息
    public Department getCurrentDepartment(Integer departmentId){
        //根据部门ID查询部门信息
        Department department = this.baseMapper.selectOne(new QueryWrapper<Department>().eq("department_id",departmentId));
        return department;
    }
    //获取当前部门下的子部门列表
    public List<Department> getChildDepartmentList(Integer departmentId){
        List<Department> childDepartmentList = new ArrayList();
        //根据部门ID查询子部门列表
        childDepartmentList = this.baseMapper.selectList(new QueryWrapper<Department>().eq("parent_id",departmentId));
        return childDepartmentList;
    }
}
